---
import type { AstroComponentFactory } from "astro/runtime/server/index.js";

import NoteCard from "@components/NoteCard.astro";
import BaseLayout from "@layouts/BaseLayout.astro";

import { getAllNotes, mdTimeFilter } from "@utils/astroUtil";
import NotePaginator from "@components/NotePaginator.astro";

export interface Props {
    page: {
        data: Array<{
            Content: AstroComponentFactory;
            frontmatter: any;
            slug: string;
        }>,
        currentPage: number,
        lastPage: number,
    };
}


export async function getStaticPaths({ paginate } : { paginate: Function }) {
    const notes = await getAllNotes()

    const noteContens = await Promise.all(
        notes
        .sort((noteBef, noteAft) => noteAft.data.pubDate.valueOf() - noteBef.data.pubDate.valueOf())
        .filter(mdTimeFilter)
        .map(async (note) => {
            const { Content } = await note.render();
            return {
                Content,
                frontmatter: note.data,
                slug: note.slug,
            };
        })
    );
    // 返回所有条目的分页路径集合
    return paginate(noteContens, { pageSize: 8 });
}


const { page } = Astro.props;
---

<BaseLayout title={ `随手一记 第${page.currentPage}页` }>
    <section class="notes-wrapper">
        {page.data.map(({ Content, frontmatter, slug }, idx) => (
            <NoteCard frontmatter={frontmatter} slug={slug}>
                <Content key={idx} />
            </NoteCard>
        ))}
        <NotePaginator currentPage={ page.currentPage } lastPage={ page.lastPage } />
    </section>
</BaseLayout>


<style lang="scss">
.notes-wrapper {
    padding-top: 40px;
}
</style>
